//time 2018-2-6
define(['knockout'], function (ko) {
    function Selector(list) {
        var self = this;
        self.list = list;
        //判断是否是全选操作同时进行对应的显示
        self.isSelectAll = ko.pureComputed({
            read: function () {
                return (this.selectedCount() === this.list().length) && (this.list().length !== 0);
            },
            write: function (value) {
                var i = 0;
                var len = this.list().length;
                if (value) {
                    this.selectAll();
                } else {
                    this.unselectAll();
                }
            },
            owner: self
        });
        //选中的数量
        self.selectedCount = ko.pureComputed(function () {
            return this.selectedList().length;
        }, self);
        //选中项列表
        self.selectedList = ko.pureComputed(function () {
            var list = [];
            var i = 0;
            var len = this.list().length;
            for (i = 0; i < len; i++) {
                var obj = this.list()[i];
                if (obj.isSelected()) {
                    list.push(obj);
                }
            }
            return list;
        }, self);
    }
    Selector.fn = Selector.prototype;
    //反选操作
    Selector.fn.invertSelect = function () {
        var i = 0;
        var len = this.list().length;
        for (; i < len; i++) {
            var o = this.list()[i];
            o.isSelected(!o.isSelected());
        }
    };
    //全选
    Selector.fn.selectAll = function () {
        var i = 0;
        var len = this.list().length;
        for (; i < len; i++) {
            this.list()[i].isSelected(true);
        }
    }
    //取消全选
    Selector.fn.unselectAll = function () {
        var i = 0;
        var len = this.list().length;
        for (; i < len; i++) {
            this.list()[i].isSelected(false);
        }
    }
    return Selector;
});